#!/bin/bash
#
# addlang
#
# Language addition helper for the Dragora GNU/Linux-Libre website
# (https://www.dragora.org)
#
#
# Copyright (C) 2021 Michael Siegel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#### INCLUDES ####

. include/constants || exit 1
. include/subroutines || exit 1


#### CONSTANTS ####

declare -r ISO_LANG_CODES=(
  aa ab ae af ak am an ar as av ay az
  ba be bg bh bi bm bn bo br bs
  ca ce ch co cr cs cu cv cy
  da de dv dz
  ee el en eo es et eu
  fa ff fi fj fo fr fy
  ga gd gl gn gu gv
  ha he hi ho hr ht hu hy hz
  ia id ie ig ii ik io is it iu
  ja jv
  ka kg ki kj kk kl km kn ko kr ks ku kv kw ky
  la lb lg li ln lo lt lu lv
  mg mh mi mk ml mn mr ms mt my
  na ne nb nd ng nl nn no nr nv ny
  oc oj om or os
  pa pi pl ps pt
  qu
  rm rn ro ru rw
  sa sc sd se sg si sk sl sm sn so sq sr st ss su sv sw
  ta te tg th ti tk tl tn to tr ts tt tw ty
  ug uk ur uz
  ve vi vo
  wa wo
  xh
  yi yo
  za zh zu
)


#### GLOBAL PARAMETERS ####

new_lang=


#### FUNCTIONS ####

_lang_valid() {

  local input="$1"

  local lang=
  for lang in "${ISO_LANG_CODES[@]}"
  do
    [[ "$lang" = "$input" ]] && return 0
  done
  unset lang

  return 1
}

_lang_exists() {

  local input="$1"

  local lang=
  for lang in $(_get_lang_dirs)
  do
    [[ "$lang" = "$input" ]] && return 0
  done
  unset lang

  return 1
}

_prompt_new_lang() {

  local input=

  while true
  do
    if [[ -z "$input" ]]
    then
      printf '%s\n' \
        'Please enter the ISO two-letter code for the language to add:'
      printf '%s\n' "$HELP_TIP_INTERACTIVE"
    fi

    read -rp "$PROMPT" input

    if [[ -z "$input" ]]
    then
      continue
    elif [[ "$input" = "$HELP_COMMAND" ]]
    then
      _show_help "$FUNCNAME"
    elif ! _lang_valid "$input"
    then
      _perr "invalid language code -- '$input'"
      printf '%s\n' "$HELP_TIP_INTERACTIVE"
    elif _lang_exists "$input"
    then
      _perr "language already exists -- '$input'"
      printf '%s\n' "$HELP_TIP_INTERACTIVE"
    else
      break
    fi
  done

  new_lang="$input"
}

_show_help() {

  case "$1" in
    global)
cat <<'EOF'
Add a new language interactively

Usage:
    addlang

Options:
    --help
        Show this help text and exit
EOF
    ;;
#    _prompt_new_lang)
    # Display complete list on help request?
#    ;;
    *)
      printf '%s\n' "Sorry, no help text available."
    ;;
  esac
}

#### MAIN ####

if [[ "$1" = '--help' ]]
then
  _show_help global
  exit
fi

## Environment checks

_env_checks || _abort

## Action

_prompt_new_lang || _abort

cp -a -- "$PAGES_DIR_MASTER" "$PAGES_DIR"/"$new_lang" || \
  { _perr "Could not add new language '$new_lang'."; _abort; }
printf '%s\n' "Added new language '$new_lang'."
